package com.landeqing.controller;

import com.landeqing.model.Address;
import com.landeqing.model.City;
import com.landeqing.model.Province;
import com.landeqing.model.Xian;
import com.landeqing.service.impl.OrderAddressServiceImpl;
import com.landeqing.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("orderAddress")
public class OrderAddressController {
    @Autowired
    private OrderAddressServiceImpl orderAddressService;
    /****
     *进入添加页面
     */
    @RequestMapping("toSave")
    public ModelAndView toSave(){
        ModelAndView mav=new ModelAndView("/addressAdd");
        List<Province> pList=orderAddressService.getProvinceList();
        mav.addObject("pList",pList);
        return mav;
    }

    /***
     * 添加收货人信息
     * @param id
     * @param userName
     * @param district
     * @param phone
     * @param address
     * @return
     */
    @RequestMapping("save")
    public  ModelAndView save(@RequestParam(value = "id",required = false) Integer id,
                             @RequestParam(value = "userName",required = false)String userName,
                             @RequestParam(value = "district",required = false) String district,
                             @RequestParam(value = "phone",required = false) String phone,
                             @RequestParam(value = "address",required = false) String address
                             ){
        Address addres=new Address();
        addres.setUserName(userName);
        addres.setDistrict(district);
        addres.setAddress(address);
        addres.setPhone(phone);
        int result=orderAddressService.save(addres);
        //ModelAndView mav=new ModelAndView("");
        return list();



    }
    @RequestMapping("list")
    public ModelAndView list(){
        ModelAndView mav=new ModelAndView("addressList");
        List<Address> list=orderAddressService.getList();
        List<AddressVo>voList=new ArrayList<AddressVo>();
        for (Address address:list
             ) {
            AddressVo vo=new AddressVo();
            vo.setId(address.getId());
            vo.setUserName(address.getUserName());
            vo.setPhone(address.getPhone());
            vo.setAddress(address.getAddress());

            String []  cityArray=address.getDistrict().split(",");

            Province province=orderAddressService.getProvinceById(Integer.parseInt(cityArray[0]));
            vo.setProvinceName(province.getP_name());
            City city=orderAddressService.getCityById(Integer.parseInt(cityArray[1]));
            vo.setCityName(city.getC_name());
            Xian xian=orderAddressService.getXianById(Integer.parseInt(cityArray[2]));
            vo.setCountyName(xian.getX_name());
            voList.add(vo);

        }
        mav.addObject("list",voList);
        return mav;
    }
    @RequestMapping("city")
    public @ResponseBody Object cityList(@RequestParam(value = "pid" ,required = false) int  pid){
        List<City> cityList=orderAddressService.getCityListById(pid);

        return cityList;
    }
    @RequestMapping("xian")
    public @ResponseBody Object xianList(@RequestParam(value = "cid" ,required = false) int  cid){
        List<Xian> xianList=orderAddressService.getXianListById(cid);
        return xianList;
    }
    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(@RequestParam(value = "id",required = false) int id){
        Address address=orderAddressService.getAddressById(id);
        String []  cityArray=address.getDistrict().split(",");
        Province province=orderAddressService.getProvinceById(Integer.parseInt(cityArray[0]));
        City city=orderAddressService.getCityById(Integer.parseInt(cityArray[1]));
        Xian xian=orderAddressService.getXianById(Integer.parseInt(cityArray[2]));
        List<Province> pList=orderAddressService.getProvinceList();
        ModelAndView mav=new ModelAndView("addressUpdate");
        mav.addObject("pList",pList);
        mav.addObject("address",address);
        mav.addObject("province",province);
        mav.addObject("city",city);
        mav.addObject("xian",xian);
        System.out.println(province.getP_name());
        System.out.println(city.getC_name());
        System.out.println(xian.getX_name());
        return  mav;
    }
    @RequestMapping("update")
    public  ModelAndView update(@RequestParam(value = "id",required = false) Integer id,
                                @RequestParam(value = "userName",required = false)String userName,
                                @RequestParam(value = "district",required = false) String district,
                                @RequestParam(value = "phone",required = false) String phone,
                                @RequestParam(value = "address",required = false) String address
    ) {
        Address addres=orderAddressService.getAddressById(id);
        addres.setId(id);
        addres.setUserName(userName);
        addres.setDistrict(district);
        addres.setAddress(address);
        addres.setPhone(phone);
        boolean result=orderAddressService.update(addres);
        if(result==true)
            return list();
        else return null;
    }
    @RequestMapping("delete")
    public ModelAndView delete(@RequestParam(value = "id",required = false) int id){
       boolean result=orderAddressService.delete(id);
        if (result==true)
            return list();
        System.out.println(result);
        return  null;
    }
}
